/**
 * 显示消息提示框
 * @param content 提示的标题
 */
// export function toast(content) {
// 	uni.showToast({
// 		icon: 'none',
// 		title: content
// 	})
// }

export function toast(content, duration, icon) {
	uni.showToast({
		icon: icon,
		title: content,
		duration: duration

	})
}
export function msg(title, duration = 2000, mask = false, icon = 'none') {
	if (Boolean(title) === false) {
		return;
	}
	uni.showToast({
		title,
		duration,
		mask,
		icon
	});
}

export function customerMsg(title, duration = 2000, mask = false, icon = 'none') {
	if (Boolean(title) === false) {
		return;
	}
	uni.showToast({
		title,
		duration,
		mask,
		icon
	});
}

/**
 * 显示模态弹窗
 * @param content 提示的标题
 */
export function showConfirm(content) {
	return new Promise((resolve, reject) => {
		uni.showModal({
			title: '提示',
			content: content,
			cancelText: '取消',
			confirmText: '确定',
			success: function(res) {
				resolve(res)
			}
		})
	})
}


/**
 * 参数处理
 * @param {*} params  参数
 */
export function tansParams(params) {
	let result = ''
	for (const propName of Object.keys(params)) {
		const value = params[propName];
		var part = encodeURIComponent(propName) + "=";
		if (value !== null && typeof(value) !== "undefined") {
			if (typeof value === 'object') {
				for (const key of Object.keys(value)) {
					if (value[key] !== null && typeof(value[key]) !== 'undefined') {
						let params = propName + '[' + key + ']';
						var subPart = encodeURIComponent(params) + "=";
						result += subPart + encodeURIComponent(value[key]) + "&";
					}
				}
			} else {
				result += part + encodeURIComponent(value) + "&";
			}
		}
	}
	return result
}

//根据身份证获取出生日期性别年龄
export function idCard(IdCard, type) {
	if (IdCard == '' || IdCard == null) {
		return ''
	}
	if (type === 1) {
		//获取出生日期
		let birthday = IdCard.substring(6, 10) + "-" + IdCard.substring(10, 12) + "-" + IdCard.substring(12, 14)
		return birthday
	}
	if (type === 2) {
		//获取性别
		if (parseInt(IdCard.substr(16, 1)) % 2 === 1) {
			return "男"
		} else {
			return "女"
		}
	}
	if (type === 3) {
		//获取年龄
		var ageDate = new Date()
		var month = ageDate.getMonth() + 1
		var day = ageDate.getDate()
		var age = ageDate.getFullYear() - IdCard.substring(6, 10) - 1
		if (IdCard.substring(10, 12) < month || IdCard.substring(10, 12) === month && IdCard.substring(12,
				14) <= day) {
			age++
		}
		if (age <= 0) {
			age = 1
		}
		return age
	}
}

//获取当前年月日
export function getCurrentDate() {
	let dd = new Date()
	var year = dd.getFullYear();
	var mon = dd.getMonth() + 1; //获取当前月份的日期
	var day = dd.getDate();
	return year + '-' + (mon < 10 ? ('0' + mon) : mon) + '-' + (day < 10 ? ('0' + day) : day);
}
//获取当前年月日
export function getDate(a) {
	let dd = new Date(a)
	var year = dd.getFullYear();
	var mon = dd.getMonth() + 1; //获取当前月份的日期
	var day = dd.getDate();
	return year + '-' + (mon < 10 ? ('0' + mon) : mon) + '-' + (day < 10 ? ('0' + day) : day);
}

export function arrNoRepeat(arr, key) {
	if (!key) {
		return [...new Set(arr)]
	}
	let obj = {}
	arr = arr.reduce((item, next) => {
		obj[next[key]] ? '' : obj[next[key]] = true && item.push(next)
		return item
	}, [])
	return arr
}
export function checkProv(val) {
	var pattern = /^[1-9][0-9]/;
	var provs = {
		11: "北京",
		12: "天津",
		13: "河北",
		14: "山西",
		15: "内蒙古",
		21: "辽宁",
		22: "吉林",
		23: "黑龙江 ",
		31: "上海",
		32: "江苏",
		33: "浙江",
		34: "安徽",
		35: "福建",
		36: "江西",
		37: "山东",
		41: "河南",
		42: "湖北 ",
		43: "湖南",
		44: "广东",
		45: "广西",
		46: "海南",
		50: "重庆",
		51: "四川",
		52: "贵州",
		53: "云南",
		54: "西藏 ",
		61: "陕西",
		62: "甘肃",
		63: "青海",
		64: "宁夏",
		65: "新疆",
		71: "台湾",
		81: "香港",
		82: "澳门"
	};
	if (pattern.test(val)) {
		if (provs[val]) {
			return true;
		}
	}
	return false;
}
export function checkDate(val) {
	var pattern =
		/^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
	if (pattern.test(val)) {
		var year = val.substring(6, 10);
		var month = val.substring(10, 12);
		var date = val.substring(12, 14);
		var date2 = new Date(year + "-" + month + "-" + date);
		if (date2 && date2.getMonth() == (parseInt(month) - 1)) {
			return true;
		}
	}
	return false;
}


//校验位的检测
export function checkParity(val) {
	var len = val.length;
	if (len == '18') {
		var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
		var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
		var cardTemp = 0,
			i, valnum;
		for (i = 0; i < 17; i++) {
			cardTemp += val.substr(i, 1) * arrInt[i];
		}
		valnum = arrCh[cardTemp % 11];
		if (valnum == val.substr(17, 1)) {
			return true;
		}
		return false;
	}
	return false;
}
//正则手机验证
export function checkPhone(val) {
	var pattern =
		/^(13[0-9]|14[0|1|2|3|4|5|6|7|8|9]|15[0|1|2|3|4|5|6|7|8|9]|16[0|1|2|3|4|5|6|7|8|9]|17[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|4|5|6|7|8|9]|19[0|1|2|3|4|5|6|7|8|9])\d{8}$/;
	if (pattern.test(val)) {
		return true;
	}
	return false;
}

//防抖
let timeout = null
export const vueDebounce = (fun, time = 500) => {
	if (timeout) {
		clearTimeout(timeout) // 规定时间内若定时器存在则清除
	}
	timeout = setTimeout(() => {
		fun() // 调用接口
	}, time);
}

//根基时间戳获取乃年月日时分
export function getTimeStamp(time, type = 0) {
	if (time) {
		let val = new Date(time);
		let Y = val.getFullYear();
		let M = (val.getMonth() + 1) < 10 ? '0' + (val.getMonth() + 1) : (val.getMonth() + 1);
		let D = val.getDate() < 10 ? '0' + val.getDate() : val.getDate();
		let h = val.getHours() < 10 ? '0' + val.getHours() : val.getHours();
		let m = val.getMinutes() < 10 ? '0' + val.getMinutes() : val.getMinutes();
		if (type == 0) {
			return Y + '-' + M + '-' + D + ' ' + h + ':' + m;
		}
		if (type == 1) {
			return Y + '-' + M + '-' + D;
		}

	}
	return ''
}

/* 获取指定日期的后七天不包括指定日期 */
export function nextSevenDay() {
	let now = new Date();
	var year = now.getFullYear(); //得到年份
	var month = now.getMonth() + 1; //得到月份
	var date = now.getDate(); //得到日期
	//从当前时间往后7天
	var check = [];
	for (let i = 1; i < 8; i++) {
		//24 * 3600 * 1000 就是计算一天的时间 
		var date = new Date(now.getTime() + i * 24 * 3600 * 1000);
		var year = date.getFullYear();
		var month = (date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : "0" + (date.getMonth() + 1);
		var day = date.getDate() > 9 ? date.getDate() : '0' + date.getDate();
		var dt2 = new Date(now.getTime() + i * 24 * 3600 * 1000);
		var weekDay = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
		var weekDays = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"];

		//把七天的时间和星期添加到数组中
		check.push({
			date: year + "-" + month + "-" + day,
			day: day,
			weekday: weekDays[dt2.getDay()],
			week: weekDay[dt2.getDay()],
		});
	}
	return check
}
/* 获取url中的参数 */
export function getQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	var r = window.location.search.substr(1).match(reg);
	if (r != null && r[2] != "false")
		return r ? decodeURIComponent(r[2]) : null;
	return false;
}
export function getSimpleDate(time, minutes = 1) {
	// 获取当前时间
	let date = new Date(time);

	// 将当前时间加上一分钟
	date.setMinutes(date.getMinutes() + minutes);


	var y = date.getFullYear();
	var m = date.getMonth() + 1;
	m = m < 10 ? ('0' + m) : m;
	var d = date.getDate();
	d = d < 10 ? ('0' + d) : d;
	var h = date.getHours();
	h = h < 10 ? ('0' + h) : h
	var minute = date.getMinutes();
	minute = minute < 10 ? ('0' + minute) : minute;
	var s = date.getSeconds();
	s = s < 10 ? '0' + s : s;
	return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + s;
}

/* 商丘入企码对接 */
export function getSq(url) {
	let id = getMidString(url, 'deptindex/')
	let paramName = ''
	if (url.includes('lypy')) { //lypy梁园
		paramName = 'lypy'
	}
	if (url.includes('mqpy')) { //mqpy民权
		paramName = 'mqpy'
	}
	if (url.includes('ycysjd')) { //ycysjd虞城
		paramName = 'ycysjd'
	}
	if (url.includes('sx.ysjd')) { //sx.ysjd宁陵、宁陵
		paramName = 'sx.ysjd'
	}
	if (url.includes('sfpy')) { //sfpy示范区、睢阳区
		paramName = 'sfpy'
	}
	if (url.includes('xypy')) { //xypy夏邑
		paramName = 'xypy'
	}
	if (url.includes('yoch')) { //yoch永城
		paramName = 'yoch'
	}
	return {
		id: id,
		paramName: paramName
	}
}

export function getMidString(str, startStr) {
	const startIdx = str.indexOf(startStr);
	if (startIdx === -1) return '';
	const startIndex = startIdx + startStr.length;
	return str.substring(startIndex, str.length);
}